home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 5 / MACVOGL- / EXAMPLES / TETRA.C < prev    next >
C/C++ Source or Header  |  1992-07-19  |  3KB  |  176 lines

  1. /*
  2.  * Demonstrate a rotating translating tetrahedron.
  3.  */
  4.  
  5. #include <stdio.h>
  6.  
  7. #ifdef SGI
  8. #include "gl.h"
  9. #include "device.h"
  10. #else
  11. #include "vogl.h"
  12. #include "vodevice.h"
  13. #endif
  14.  
  15. #ifndef TC
  16. #include <math.h>
  17. #else
  18. extern double sin(), cos();
  19. #endif
  20.  
  21. #define    TETRAHEDRON    1L
  22. #define    NSIDES    3
  23. #define    NFACES    4
  24. #define    NPNTS    4
  25.  
  26. Coord    points[NPNTS][3] = {
  27.     {-0.5, 0.866, -0.667},
  28.     {-0.5, -0.866, -0.667},
  29.     { 1.0, 0.0, -0.667},
  30.     { 0.0, 0.0, 1.334}
  31. };
  32.  
  33. int    faces[NFACES][NSIDES] = {
  34.     {2, 1, 0},
  35.     {0, 1, 3},
  36.     {1, 2, 3},
  37.     {2, 0, 3}
  38. };
  39.  
  40. int    colface[NFACES] = {
  41.         GREEN,
  42.         YELLOW,
  43.         CYAN,
  44.         MAGENTA
  45. };
  46.  
  47. main(argc, argv)
  48.     int    argc;
  49.     char    **argv;
  50. {
  51.     char    dev[20];
  52.     int    i, but;
  53.     int    rotval = 0, drotval = 2;
  54.     float    R = 1.6, tx = 0.0, tz = R, zeye = 5.0;
  55.     int    do_backface = 0;
  56.     int    do_fill = 0;
  57.     short    val;
  58.  
  59.     for (i = 1; i < argc; i++) {
  60.         if (!strcmp(argv[i], "-b"))
  61.             do_backface = 1;
  62.         if (!strcmp(argv[i], "-f"))
  63.             do_fill = 1;
  64.     }
  65.  
  66.     prefsize(400L, 400L);
  67.  
  68.     winopen("tetra");          /* set up device */
  69.  
  70.     qdevice(ESCKEY);
  71.     qdevice(QKEY);
  72.     unqdevice(INPUTCHANGE);
  73.  
  74.     doublebuffer();
  75.     gconfig();
  76.  
  77.  
  78.     polymode(PYM_LINE);
  79.     if (do_fill)
  80.         polymode(PYM_FILL);
  81.  
  82.     if (do_backface)
  83.         backface(1);
  84.  
  85.     /*
  86.      * set up a perspective projection with a field of view of
  87.      * 40.0 degrees, aspect ratio of 1.0, near clipping plane 0.1,
  88.      * and the far clipping plane at 1000.0.
  89.      */
  90.     perspective(400, 1.0, 0.001, 15.0);
  91.     lookat(0.0, 0.0, zeye, 0.0, 0.0, 0.0, 0);
  92.  
  93.  
  94.     /*
  95.      * Make a tetrahedron object
  96.      */
  97.  
  98.     maketetra();
  99.  
  100.     do {
  101.         for (rotval = 0; rotval < 3600; rotval += drotval) {
  102.             color(BLACK);
  103.             clear();
  104.  
  105.             /*
  106.              * Rotate the whole scene...(this acumulates - hence
  107.              * drotval)
  108.              */
  109.             rotate(drotval, 'x');
  110.             rotate(drotval, 'z');
  111.  
  112.             color(RED);
  113.             pushmatrix();
  114.                 rotate(900, 'x');
  115.                 circ(0.0, 0.0, R);
  116.             popmatrix();
  117.  
  118.             color(BLUE);
  119.             move(0.0, 0.0, 0.0);
  120.             draw(tx, 0.0, tz);
  121.             
  122.             /*
  123.              * Remember! The order of the transformations is
  124.              * the reverse of what is specified here in between
  125.              * the pushmatrix and the popmatrix. These ones don't
  126.              * accumulate because of the push and pop.
  127.              */
  128.             pushmatrix();
  129.                 translate(tx, 0.0, tz);
  130.                 rotate(rotval, 'x');
  131.                 rotate(rotval, 'y');
  132.                 rotate(rotval, 'z');
  133.                 scale(0.4, 0.4, 0.4);
  134.                 callobj(TETRAHEDRON);
  135.             popmatrix();
  136.  
  137.             tz = R * cos((double)(rotval * 3.1415926535 / 180));
  138.             tx = R * sin((double)(rotval * 3.1415926535 / 180));
  139.  
  140.             swapbuffers();
  141.  
  142.             if (qtest()) {
  143. /*
  144.                 but = (int)qread(&val);
  145.                 fprintf(stderr, "but = %c (%d)\n", but, but);
  146. */
  147.                 gexit();
  148.                 exit(0);
  149.             }
  150.         }
  151.  
  152.     } while (1);
  153. }
  154.  
  155. /*
  156.  * maketetra
  157.  *
  158.  *    draw a tetrahedron
  159.  */
  160. maketetra()
  161. {
  162.     int    i, j;
  163.  
  164.     makeobj(TETRAHEDRON);
  165.  
  166.     for (i = 0; i < NFACES; i++) {
  167.         color(colface[i]);
  168.         bgnpolygon();
  169.             for (j = 0; j < NSIDES; j++) 
  170.                 v3f(points[faces[i][j]]);
  171.         endpolygon();
  172.     }
  173.  
  174.     closeobj();
  175. }
  176.